
#include <iostream>

//实现类接口
class Game
{
public:
	Game() {};
	virtual void setName(char* iName) = 0;
	virtual void play() = 0;
public:
	char* name;
};

//具体实现类GameA
class GameA :public Game
{
public:
	GameA() {
	}
	void setName(char* iName) {
		this->name = iName;
		printf("Create GameA : %s...%08x\n", this->name, &(this->name));
	}
	void play() {
		printf("Jungle玩游戏A\n");
	}
};

//具体实现类GameB
class GameB :public Game
{
public:
	GameB() {
	}
	void setName(char* iName) {
		this->name = iName;
		printf("Create GameB : %s...%08x\n", this->name, &(this->name));
	}
	void play() {
		printf("Jungle玩游戏B\n");
	}
};

//抽象类Phone
class Phone
{
public:
	Phone() {
	}
	//安装游戏
	virtual void setupGame(Game* igame) = 0;
	virtual void play() = 0;
private:
	Game* game;
};

//扩充抽象类PhoneA
class PhoneA :public Phone
{
public:
	PhoneA() {
	}
	//安装游戏
	void setupGame(Game* igame) {
		printf("setup %s on PhoneA..%08x.\n", (igame->name), &(igame->name));
		this->game = igame;
	}
	void play() {
		this->game->play();
	}
private:
	Game* game;
};

//扩充抽象类PhoneB
class PhoneB :public Phone
{
public:
	PhoneB() {
	}
	//安装游戏
	void setupGame(Game* igame) {
		printf("setup %s on PhoneB..%08x.\n", (igame->name), &(igame->name));
		this->game = igame;
	}
	void play() {
		this->game->play();
	}
private:
	Game* game;
};

int main()
{
	Game* game;
	Phone* phone;

	//Jungle买了PhoneA品牌的手机，想玩游戏A
	phone = new PhoneA();
	game = new GameA();
	game->setName((char *)"AAA");
	phone->setupGame(game);
	phone->play();
	printf("++++++++++++++++++++++++++++++++++\n");

	//Jungle想在这个手机上玩游戏B
	game = new GameB();
	game->setName((char*)"BBB");
	phone->setupGame(game);
	phone->play();


	printf("\n\n----=----=----=----=----=----=----\n\n");

	phone = new PhoneB();
	game = new GameA();
	game->setName((char*)"AAA");
	phone->setupGame(game);
	phone->play();
	printf("++++++++++++++++++++++++++++++++++\n");

	//Jungle想在这个手机上玩游戏B
	game = new GameB();
	game->setName((char*)"BBB");
	phone->setupGame(game);
	phone->play();
	return 0;
}

